/**
 * brand controller js code
 */
define(function(require, exports) {

	//加载依赖
	require("ajaxproxy");
	var common = require("common");
	var template = require("jtemplate");
	template.config("openTag", "{[");
	template.config("closeTag", "]}");

	exports.init = function(configs) {

		//初始化swich插件
		common.bswitctListInit(configs);
		common.bswitchAddInit();

		//添加数据
		$("#content-add").on("click", function() {
			common.post("添加品名", "content-add-template", {width:600, height:420}, {status:'1', index_show: 1}, configs.insert_url);
		});

		//编辑数据
		$(".item-edit").on("click", function() {

			$.get(configs.edit_url, {
				id : $(this).data("id")
			}, function(res) {
				if ( res.code == '000' ) {

					common.post("编辑品名", "content-add-template", {width:600, height:420}, res.data, configs.update_url);

				} else {
					common.message({
						type: __global.jdialog.messageType[res.code],
						content:res.message,
						lock : true
					});
				}
			}, 'json')

		});

		//批量审核
		$("#enabled").on("click", function() {
			exports.set(configs, this);
		});

		$("#disabled").on("click", function() {
			exports.set(configs, this);
		});

		$('#remove').on("click", function() {

			if ( $('#J_List_Form').find('input[name="ids\[\]"]:checked').length == 0 ) {
				common.message({
					type:"warn",
					top : 70,
					content:"请至少选中一条记录"
				});
				return;
			}

			var formData = $("#J_List_Form").serializeArray();
			var button = $(this).button("loading"); //锁定按钮防止重复点击
			$.post(configs.dels_url, formData, function(data) {
				if ( data.code == '000' ) {
					common.message({
						type:"ok",
						content:data.message,
						lock:true
					});

					//刷新页面
					setTimeout(function() {
						location.reload();
					}, __global.jdialog.timer)
				} else {
					common.message({
						type:"error",
						top : 70,
						content:data.message,
						lock:false
					});

					button.button("reset");
				}
			}, 'json');

		});
	}

	//启用|禁用操作
	exports.set = function(configs, obj) {

		if ( $('#J_List_Form').find('input[name="ids\[\]"]:checked').length == 0 ) {
			common.message({
				type:"warn",
				top : 70,
				content:"请至少选中一条记录"
			});
			return;
		}

		var formData = $("#J_List_Form").serializeArray();
		formData.push({name:'field', value:'status'});
		formData.push({name:'value', value:$(obj).data("value")});
		//console.log(formData);
		$(obj).button("loading"); //锁定按钮防止重复点击
		$.post(configs.sets_url, formData, function(data) {
			if ( data.code == '000' ) {
				common.message({
					type:"ok",
					content:data.message,
					lock:true
				});

				//刷新页面
				setTimeout(function() {
					location.reload();
				}, __global.jdialog.timer)
			} else {
				common.message({
					type:"error",
					top : 70,
					content:data.message,
					lock:false
				});
				$(obj).button("reset");
			}
		}, 'json');

	}

});
